package com.xdtech.hotel.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thoughtworks.xstream.XStream;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.DeviceLinkDao;
import com.xdtech.hotel.model.DeviceLink;
import com.xdtech.hotel.model.DeviceLinkOption;
import com.xdtech.hotel.service.DeviceLinkOptionService;
import com.xdtech.hotel.service.DeviceLinkService;
import com.xdtech.hotel.vo.DeviceLinkItem;
import com.xdtech.sys.model.CodeValue;
import com.xdtech.sys.vo.CodeValueItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:12:27
 * @since 1.0
 * @see
 */
@Service
public class DeviceLinkServiceImpl implements DeviceLinkService {
	@Autowired
	private DeviceLinkDao deviceLinkDao;
	@Autowired
	private DeviceLinkOptionService deviceLinkOptionService;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param entity
	 */
	public void save(DeviceLink entity) {
		deviceLinkDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<DeviceLink> entities) {
		deviceLinkDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param entity
	 */
	public void delete(DeviceLink entity) {
		deviceLinkDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		deviceLinkDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param id
	 * @return
	 */
	public DeviceLink get(Long id) {
		return deviceLinkDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @return
	 */
	public List<DeviceLink> getAll() {
		return deviceLinkDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> deviceLinks = null;
		long rows = 0;
		if (pg!=null) {
			Page<DeviceLink> page = deviceLinkDao.findPage(pg,"from DeviceLink order by createTime desc", values);
			deviceLinks = BeanUtils.copyListProperties(
					DeviceLinkItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<DeviceLink> deviceLinkList = deviceLinkDao.find("from DeviceLink order by id desc", values);
			deviceLinks = BeanUtils.copyListProperties(
					DeviceLinkItem.class, deviceLinkList);
			rows = deviceLinks.size();
		}
		results.put("total", rows);
		results.put("rows", deviceLinks);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateDeviceLink(DeviceLinkItem item) {
		DeviceLink deviceLink = null;
		if (item.getId()==null) {
			deviceLink = new DeviceLink();
		}else {
			deviceLink = deviceLinkDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(deviceLink, item);
		deviceLinkDao.save(deviceLink);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param deviceLinkId
	 * @return
	 */
	public DeviceLinkItem loadDeviceLinkItem(Long deviceLinkId) {
		DeviceLink deviceLink = deviceLinkDao.get(deviceLinkId);
		DeviceLinkItem deviceLinkItem = DeviceLinkItem.createItem(deviceLink);
		return deviceLinkItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteDeviceLinkInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:12:27
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteDeviceLinkInfo(List<Long> deviceLinkIds) {
		for (Long id : deviceLinkIds) {
			deviceLinkOptionService.deleteByLinkId(id);
			deviceLinkDao.batchExecute("delete from DeviceLink where id=?", id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-5下午1:35:49
	 * @modified by
	 * @param deviceLink
	 */
	@Override
	public void saveDeviceLinkOptions(DeviceLink deviceLink) {
		deviceLinkDao.save(deviceLink);
		//不管是更新还是新增，都统一删除动作
//		deviceLinkOptionService.deleteByLinkId(deviceLink.getId());
//		List<DeviceLinkOption> deviceLinkOptions = deviceLink.getDeviceLinkOptions();
//		for (DeviceLinkOption deviceLinkOption : deviceLinkOptions) {
//			deviceLinkOption.setDeviceLink(deviceLink);
//			//然后重置id为空
//			deviceLinkOption.setId(null);
//			deviceLinkOptionService.save(deviceLinkOption);
//		}
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-9下午11:24:26
	 * @modified by
	 * @param type
	 * @return
	 */
	@Override
	public DeviceLinkItem loadDeviceLinkItemByType(String type) {
		DeviceLink deviceLink = deviceLinkDao.findUnique("from DeviceLink where ltype=?", type);
		return DeviceLinkItem.createItem(deviceLink);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:16:56
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-22下午2:49:32
	 * @modified by
	 * @param deviceLinks
	 */
	@Override
	public void updateDeviceLinks(List<DeviceLinkItem> deviceLinks) {
		DeviceLink deviceLink = null;
		for (DeviceLinkItem deviceLinkItem : deviceLinks) {
			if (deviceLinkItem.getId()==null) {
				deviceLink = new DeviceLink();
//				deviceLink.setComponent(deviceLinkItem.getComp());
			}else {
				deviceLink = deviceLinkDao.get(deviceLinkItem.getId());
			}
			BeanUtils.copyProperties(deviceLink, deviceLinkItem);
			deviceLink.setComponent(new XStream().toXML(deviceLinkItem.getComp()));
			deviceLinkDao.save(deviceLink);
		}
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-22下午4:16:06
	 * @modified by
	 * @param id
	 * @return
	 */
	@Override
	public DeviceLinkItem getDeviceLinkItem(Long id) {
		DeviceLink deviceLink = deviceLinkDao.findUnique("from DeviceLink where id=?",id);
		if (deviceLink==null) {
			return null;
		}
		return DeviceLinkItem.createItem(deviceLink);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-22下午5:06:59
	 * @modified by
	 * @param ids
	 * @return
	 */
	@Override
	public List<DeviceLinkItem> getDeviceLinkItems(List<Long> ids) {
		List<DeviceLinkItem> items = new ArrayList<DeviceLinkItem>();
		for (Long id : ids) {
			DeviceLinkItem item = getDeviceLinkItem(id);
			if (item!=null) {
				items.add(item);
			}
			
		}
		return items;
	}

}
